package com.xr.web.controller.miniprogram;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xr.common.annotation.Log;
import com.xr.common.core.controller.BaseController;
import com.xr.common.core.domain.AjaxResult;
import com.xr.common.enums.BusinessType;
import com.xr.miniprogram.domain.MiniProgramAgreement;
import com.xr.miniprogram.service.IMiniProgramAgreementService;
import com.xr.common.utils.poi.ExcelUtil;
import com.xr.common.core.page.TableDataInfo;

/**
 * 协议Controller
 * 
 * @author wls
 * @date 2023-12-15
 */
@Api("协议模块接口")
@RestController
@RequestMapping("/mini/agreement")
public class MpAgreementController extends BaseController
{
    @Autowired
    private IMiniProgramAgreementService miniProgramAgreementService;

    /**
     * 查询协议列表，默认只查询最新版本数据
     */
    @ApiOperation("查询协议列表")
    @PreAuthorize("@ss.hasPermi('mini:agreement:list')")
    @GetMapping("/list")
    public TableDataInfo list(MiniProgramAgreement miniProgramAgreement)
    {
        startPage();
        List<MiniProgramAgreement> list = miniProgramAgreementService.selectAgreementList(miniProgramAgreement);
        return getDataTable(list);
    }

    /**
     * 导出协议列表
     */
    @ApiOperation("导出协议列表")
    @PreAuthorize("@ss.hasPermi('mini:agreement:export')")
    @Log(title = "协议", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MiniProgramAgreement miniProgramAgreement)
    {
        List<MiniProgramAgreement> list = miniProgramAgreementService.selectAgreementList(miniProgramAgreement);
        ExcelUtil<MiniProgramAgreement> util = new ExcelUtil<MiniProgramAgreement>(MiniProgramAgreement.class);
        util.exportExcel(response, list, "协议数据");
    }

    /**
     * 获取协议详细信息
     */
    @ApiOperation("获取协议详细信息")
    @PreAuthorize("@ss.hasPermi('mini:agreement:query')")
    @GetMapping(value = "/{agreementId}")
    public AjaxResult getInfo(@PathVariable("agreementId") Long agreementId)
    {
        return success(miniProgramAgreementService.selectAgreementById(agreementId));
    }

    /**
     * 新增协议
     */
    @ApiOperation("新增协议")
    @PreAuthorize("@ss.hasPermi('mini:agreement:add')")
    @Log(title = "协议", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MiniProgramAgreement miniProgramAgreement)
    {
        return toAjax(miniProgramAgreementService.insertAgreement(miniProgramAgreement));
    }

    /**
     * 修改协议（插入新的数据，维护版本号）
     */
    @ApiOperation("修改协议")
    @PreAuthorize("@ss.hasPermi('mini:agreement:edit')")
    @Log(title = "协议", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MiniProgramAgreement miniProgramAgreement)
    {
        Date date = new Date();
        miniProgramAgreement.setCreateTime(date);//修改数据==添加新的数据
        miniProgramAgreement.setCreateBy(getUsername());
        miniProgramAgreement.setVersion(new SimpleDateFormat("yyyy-MM-dd").format(date));//以年月日为版本号
        return toAjax(miniProgramAgreementService.insertAgreement(miniProgramAgreement));
    }

    /**
     * 删除协议
     */
    @ApiOperation("删除协议")
    @PreAuthorize("@ss.hasPermi('mini:agreement:remove')")
    @Log(title = "协议", businessType = BusinessType.DELETE)
	@DeleteMapping("/{agreementIds}")
    public AjaxResult remove(@PathVariable Long[] agreementIds)
    {
        return toAjax(miniProgramAgreementService.deleteAgreementByAgreementIds(agreementIds));
    }
}
